<?php
/**
 *     interface ArrayAccess {  
        function offsetExists($key);  //被isset调用时  
        function offsetGet($key);  //被当作数组读时  
        function offsetSet($key, $value);  //被当作数组赋值时  
        function offsetUnset($key);  //被当作数组unset时  
    }  
 *
 */
class Clist {
	/**
	 * @var array internal data storage
	 */
	public $_d=array();
	/**
	 * @var integer number of items
	 */
	private $_c=0;
	/**
	 * @var boolean whether this list is read-only
	 */
	private $_r=false;
	
	public function __construct($readonly = false){
		$this->setReadOnly($readonly);
	}
	
	/**
	 * @param boolean $value whether this list is read-only or not
	 */
	public function setReadOnly($value){
		$this->_r = $value;
	}
	
	/**
	 * 增加一个item到list
	 * Enter description here ...
	 */
	public function add($item){
		$this->insertAt($this->_c,$item);
		return $this->_c-1;
	}
	
	public function insertAt($index,$item){
		if(!$this->_r){			
			if($index===$this->_c){
				$this->_d[$this->_c++]=$item;
			}				
			else if($index>=0 && $index<$this->_c)
			{
				array_splice($this->_d,$index,0,array($item));
				$this->_c++;
			}
		}
	}
}